﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Models
{
    public class MDanhMuc
    {
        private _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();
        public List<DanhMuc> FindAll()
        {
            var v = from f in context.DanhMucs orderby f.ThuTu ascending select f;
            return v.ToList();
        }

        public List<DanhMuc> FindAll_Web()
        {
            var v = from f in context.DanhMucs.Where(d=>d.HienThi==true) orderby f.ThuTu ascending select f;
            return v.ToList();
        }

        public string GetTitleByMa(int ma)
        {
            var v = from f in context.DanhMucs.Where(d => d.MaDanhMuc == ma) select f.TenDanhMuc;
            return v.FirstOrDefault();
        }
        public int GetTotal()
        {
            return FindAll().Count();
        }

      
        public string TenDanhMuc(int madm)
        {
            var v = from f in context.DanhMucs.Where(d => d.MaDanhMuc == madm) select f.TenDanhMuc;
            return v.SingleOrDefault();
        }

        public void Save(DanhMuc lsp)
        {
            context.DanhMucs.InsertOnSubmit(lsp);
            context.SubmitChanges();
        }
        public void Update(DanhMuc lsp)
        {
            var v = from f in context.DanhMucs
                    select f;
            DanhMuc lspdb = v.SingleOrDefault(d => d.MaDanhMuc.Equals(lsp.MaDanhMuc));
            lspdb.TenDanhMuc = lsp.TenDanhMuc;
            lspdb.ThuTu = lsp.ThuTu;
            context.SubmitChanges();
        }

        public string Delete(int ma)
        {
            try
            {
                //var vdmc = from p in context.DanhMucCons
                //        where p.MaDanhMuc.Equals(ma)
                //        select p;
                //DanhMucCon dmc = vdmc.SingleOrDefault();
                //context.DanhMucCons.DeleteOnSubmit(dmc);
                //context.SubmitChanges();


                var v = from p in context.DanhMucs
                        where p.MaDanhMuc.Equals(ma)
                        select p;
                DanhMuc spdb = v.SingleOrDefault();
                context.DanhMucs.DeleteOnSubmit(spdb);
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public string HienThi(int id)
        {
            try
            {
                var v = from f in context.DanhMucs
                        select f;
                DanhMuc lspdb = v.SingleOrDefault(d => d.MaDanhMuc.Equals(id));
                if (lspdb.HienThi == true)
                {
                    lspdb.HienThi = false;
                }
                else lspdb.HienThi = true;
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

        //public void Delete(int id)
        //{
        //    var v = from p in context.DanhMucs
        //            where p.MaDanhMuc.Equals(id)
        //            select p;
        //    DanhMuc spdb = v.SingleOrDefault();
        //    context.DanhMucs.DeleteOnSubmit(spdb);
        //    context.SubmitChanges();
        //}
        public void SaveChange()
        {
        }
        //public void HienThi(int id)
        //{
        //    var v = from f in context.DanhMucs
        //            select f;
        //    DanhMuc lspdb = v.SingleOrDefault(d => d.MaDanhMuc.Equals(id));
        //    if (lspdb.HienThi == true)
        //    {
        //        lspdb.HienThi = false;
        //    }
        //    else lspdb.HienThi = true;
        //    context.SubmitChanges();
        //}
        public DanhMuc GetByID(int id)
        {
            return (FindAll().Where(d => d.MaDanhMuc == id)).SingleOrDefault();
        }

        public List<DanhMuc> GetList(int start, int offset)
        {
            var v = new List<DanhMuc>();

            v = FindAll().Skip(start).Take(offset).ToList();

            return v;
        }
        public int GetMax()
        {
            return (from t in context.DanhMucs
                    orderby t.MaDanhMuc descending
                    select t.MaDanhMuc).FirstOrDefault();
        }
        public int? GetMaxTT()
        {
            return (from t in context.DanhMucs
                    orderby t.ThuTu descending
                    select t.ThuTu).FirstOrDefault();
        }
    }
}